﻿using System;
using System.Collections.Generic;
using System.Text;
using CookComputing.XmlRpc;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Framework;
using NLBS.Security;
using System.Web.Security;
namespace NLBS.Web.HttpHandlers
{



    public class RPCService : XmlRpcService, IMetaWeblog

    {
        #region IMetaWeblog Members

        public UserBlog[] getUsersBlogs(string appKey, string username, string password)
        {
            if (Login(username, password))  //验证用户名密码, 你必须在这个类中自己实现这个方法
            {
                UserBlog blog = new UserBlog();
                blog.blogid = "";  //就一个Blog, 这个不用
                blog.blogName = BlogInfo.Instance().Title; //BlogName, 随便写吧
                blog.url = BlogInfo.Instance().BaseUrl; //Blog的网址
                return new UserBlog[] { blog };
            }
            return null;

        }

        private bool Login(string username, string password)
        {
            bool isLogin= DbProvider.Instance().ValidateUser(username, StringSecurity.StringToSHA1Hash(password));
            //FormsAuthentication.SetAuthCookie(username, true);
            int power = DbProvider.Instance().GetUserByName(username).Group.Power.Post;
            if (power <= 0)
            {
                isLogin = false;
            }
            return isLogin;
        }

        public object editPost(string postid, string username, string password, Post post, bool publish)
        {
            Post editpost=new Post();
            if (Login(username, password))
            {
                editpost = getPost(postid, username, password);
            }
            return editpost;
        }

        public CategoryInfo[] getCategories(string blogid, string username, string password)
        {
            List<CategoryInfo> catList = new List<CategoryInfo>();
            if (Login(username, password))
            {

                foreach (NLBS.Entity.Category item in CategoryData.GetCategory())
                {
                    CategoryInfo catInfo = new CategoryInfo();
                    catInfo.categoryid = item.CategoryID.ToString();
                    catInfo.title = item.Name;
                    catInfo.rssUrl = string.Format("http://www.clingingboy.cn/feed.aspx?cat={0}", item.CategoryID);
                    catInfo.description = item.Name;
                    catInfo.htmlUrl = string.Format("http://www.clingingboy.cn/default.aspx?cat={0}", item.CategoryID);
                    catList.Add(catInfo);
                }
            }
            return catList.ToArray();
        }


        public bool deletePost(string appkey, string postid, string username, string password, bool publish)
        {
            if (Login(username, password))
            {
                ArticleData.DeleteArticle(int.Parse(postid));
            }
            return true;
        }

        public Post getPost(string postid, string username, string password)
        {
            Post post = new Post();
            if (Login(username, password))
            {
                Article art = DbProvider.Instance().GetArticleByID(int.Parse(postid));

                post.categories = new string[] { art.CategoryID.ToString() };
                post.dateCreated = art.PostTime;
                post.postid = art.ArticleID;
                post.userid = art.Author;
                post.title = art.Title;
                post.description = art.Content1;

            }
            return post;
        }

        public Post[] getRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {
            List<Post> postList = new List<Post>();
            if (Login(username, password))
            {
                IList<Article> artList = ArticleData.GetTopArticle(numberOfPosts);
              foreach (Article art in artList)
              {
                  Post post = new Post();
                  post.categories = new string[] { art.CategoryID.ToString() };
                  post.dateCreated = art.PostTime;
                  post.postid = art.ArticleID;
                  post.userid = art.Author;
                  post.title = art.Title;
                  post.description = art.Content1;
                  
                  postList.Add(post);

              }
            }
            
            return postList.ToArray();
        }

        public string newPost(string blogid, string username, string password, Post post, bool publish)
        {
            if (Login(username, password))
            {
                List<Category> catList = CategoryData.GetCategory();
                int catID = 0;
                foreach (Category item in catList)
                {
                    if (item.Name == post.categories[0])
                    {
                        catID = item.CategoryID;
                    }
                }
                Article art = new Article();
                art.CategoryID = catID;
                art.Title = post.title;
                art.Content0 = post.description;
                art.Content1 = post.description;
                art.PostTime = post.dateCreated;
                art.Selected = false;
                art.IsTop = false;
                art.Locked = false;
                art.Mode = 1;
                art.Tags = new List<string>();
                art.IP = RequestWrapper.IP;
                art.EnableInRss = true;
                //art.EnableComment = true;
                
                User user = DbProvider.Instance().GetUserByName(username);
                art.AuthorID = user.UserID;
                art.EditMark = string.Empty;
                art.UbbFlags = "111111";
                art.Author = username;
                art.TrackbackURL = string.Empty;
                ArticleData.InsertArticle(art);
                return "";
            }
            return "";
        }

        public UrlData newMediaObject(string blogid, string username, string password, FileData file)
        {
            return new UrlData(); ;
        }

     



        #endregion
    }
}
